package com.app.utils.util;


import android.content.Context;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;



/**
 * 和控件大小相关的工具类（改变控件大小和得到控件大小）
 *
 * @author xuxinghai   20160613
 */
public class ViewSizeUtils {

    private static int widthPixels;//屏幕的宽
    private static int heightPixels;//屏幕的高
    private static int densityDpi;//当前设备的dpi
    /**
     * 设定控件的宽度
     *
     * @param size
     * @return
     */
    public static float getWidthSize(float size) {

        if (widthPixels == 0)
            getScreenSize();
        return (widthPixels / 600.00f) * size;
    }

    /**
     * 设置控件的高度
     *
     * @param size
     * @return
     */
    public static float getHeightSize(float size) {
        if (heightPixels == 0)
            getScreenSize();
        return (heightPixels / 1000.00f) * size;

    }

    /**
     * DP转成像素
     *
     * @param context
     * @param dp
     * @return
     */
    public static int dp2px(Context context, float dp) {
        if (context == null) {
            context = AppUtils.getInstance().getContext();
        }
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dp * scale + 0.5f);
    }

    /**
     * 将sp值转换为px值，保证文字大小不变
     *
     * @param context
     * @param spValue （DisplayMetrics类中属性scaledDensity）
     * @return
     */
    public static int sp2px(Context context, float spValue) {
        if (context == null) {
            context = AppUtils.getInstance().getContext();
        }
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }

    /**
     * px转成dp
     *
     * @param context
     * @param px
     * @return
     */
    public static float px2dp(Context context, float px) {
        if (context == null) {
            context = AppUtils.getInstance().getContext();
        }
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (px / scale + 0.5f);
    }

    /**
     * 将px值转换为sp值，保证文字大小不变
     *
     * @param context
     * @param pxValue
     *            （DisplayMetrics类中属性scaledDensity）
     * @return
     */
    public static int px2sp(Context context, float pxValue) {
        if (context == null) {
            context = AppUtils.getInstance().getContext();
        }
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (pxValue / fontScale + 0.5f);
    }


    /**
     * 使传进的view横向充满全屏 重向相应扩大 线性布局
     */

    public static void chengeViewFullScreenL(View view) {
        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) view.getLayoutParams();
        int w = View.MeasureSpec.makeMeasureSpec(0,
                View.MeasureSpec.UNSPECIFIED);
        int h = View.MeasureSpec.makeMeasureSpec(0,
                View.MeasureSpec.UNSPECIFIED);
        view.measure(w, h);
        int height = view.getMeasuredHeight();
        int width = view.getMeasuredWidth();
        layoutParams.height = (int) (((float) ViewSizeUtils.getWidthPixels() / (float) width) * (float) height);
        layoutParams.width = ViewSizeUtils.getWidthPixels();
    }

    /**
     * 使传进的view横向充满全屏  带间距 重向相应扩大 线性布局
     */

    public static void chengeViewFullScreenL(View view, int px) {
        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) view.getLayoutParams();
        int w = View.MeasureSpec.makeMeasureSpec(0,
                View.MeasureSpec.UNSPECIFIED);
        int h = View.MeasureSpec.makeMeasureSpec(0,
                View.MeasureSpec.UNSPECIFIED);
        view.measure(w, h);
        int height = view.getMeasuredHeight();
        int width = view.getMeasuredWidth();
        layoutParams.height = (int) ((((float) ViewSizeUtils.getWidthPixels() - px) / (float) width) * (float) height);
        layoutParams.width = ViewSizeUtils.getWidthPixels() - px;
    }

    /**
     * 使传进的view横向充满全屏 重向相应扩大 线性布局
     */

    public static void chengeViewFullScreenL1(View view) {
        int w = View.MeasureSpec.makeMeasureSpec(0,
                View.MeasureSpec.UNSPECIFIED);
        int h = View.MeasureSpec.makeMeasureSpec(0,
                View.MeasureSpec.UNSPECIFIED);
        view.measure(w, h);
        int height = view.getMeasuredHeight();
        int width = view.getMeasuredWidth();
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewSizeUtils.getWidthPixels(), (int) (((float) ViewSizeUtils.getWidthPixels() / (float) width) * (float) height));
        view.setLayoutParams(layoutParams);
    }

    /**
     * 使传进的view横向充满全屏 重向相应扩大 ViewGroup布局
     */

    public static void chengeViewFullScreenV(View view) {
        LayoutParams layoutParams = (LinearLayout.LayoutParams) view.getLayoutParams();
        int w = View.MeasureSpec.makeMeasureSpec(0,
                View.MeasureSpec.UNSPECIFIED);
        int h = View.MeasureSpec.makeMeasureSpec(0,
                View.MeasureSpec.UNSPECIFIED);
        view.measure(w, h);
        int height = view.getMeasuredHeight();
        int width = view.getMeasuredWidth();
        layoutParams.height = (int) (((float) ViewSizeUtils.getWidthPixels() / (float) width) * (float) height);
        layoutParams.width = ViewSizeUtils.getWidthPixels();

    }

    /**
     * 使传进的view横向充满全屏 纵向相应扩大 相对布局
     */

    public static void chengeViewFullScreenR(View view) {
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view
                .getLayoutParams();
        int w = View.MeasureSpec.makeMeasureSpec(0,
                View.MeasureSpec.UNSPECIFIED);
        int h = View.MeasureSpec.makeMeasureSpec(0,
                View.MeasureSpec.UNSPECIFIED);
        view.measure(w, h);
        int height = view.getMeasuredHeight();
        int width = view.getMeasuredWidth();
        layoutParams.height = (int) (((float) ViewSizeUtils.getWidthPixels() / (float) width) * (float) height);
        layoutParams.width = ViewSizeUtils.getWidthPixels();
    }


    /**
     * 测量屏幕的宽度和高度
     */
    private static void getScreenSize() {
        DisplayMetrics displayMetrics = new DisplayMetrics();
        WindowManager windowManager = (WindowManager) AppUtils.getInstance().getContext().getSystemService(Context.WINDOW_SERVICE);
        windowManager.getDefaultDisplay().getMetrics(displayMetrics);
        widthPixels = displayMetrics.widthPixels;
        heightPixels = displayMetrics.heightPixels;
        densityDpi = displayMetrics.densityDpi;
    }




    /**
     * 得到屏幕的宽度
     */
    public static int getWidthPixels() {
        if (widthPixels == 0) {
            getScreenSize();
        }
        return widthPixels;
    }

    /**
     * 得到屏幕的高度
     */
    public static int getHeightPixels() {
        if (heightPixels == 0) {
            getScreenSize();
        }
        return heightPixels;
    }


    /**
     * 得到屏幕的dpi
     */
    public static int getScreenDpi() {
        if (densityDpi == 0) {
            getScreenSize();
        }
        return densityDpi;
    }

    /**
     * 不改变控件位置，修改控件大小(先测量再改变)
     *
     * @param view
     * @param W
     * @param H
     */
    public static void changeWH(View view, int W, int H) {
        LayoutParams params = view.getLayoutParams();
        params.height = (int) getHeightSize(H);
        params.width = (int) getWidthSize(W);
        view.requestLayout();
        view.invalidate();
    }


    /**
     * 根据输入的宽和高按比例更改图片的宽和高
     *
     * @param view
     * @param width
     * @param height
     */
    public static void changeViewAuto1080(View view, int width, int height) {
        if(view == null){
            return;
        }
        LayoutParams params = (LayoutParams) view.getLayoutParams();
        if (widthPixels == 0)
            getScreenSize();
        params.width = (int) ((widthPixels / 1080.00f) * width);
        params.height = (int) ((widthPixels / 1080.00f) * height);
//	    view.setLayoutParams(params);
        view.requestLayout();
        view.invalidate();
    }

    /**
     * 按屏幕大小比例修改控件的高 宽不变
     *
     * @param view
     * @param hight
     */
    public static void changeH(View view, int hight) {
        if(view == null){
            return;
        }
        LayoutParams params = view.getLayoutParams();
        int w = View.MeasureSpec.makeMeasureSpec(0,
                View.MeasureSpec.UNSPECIFIED);
        int h = View.MeasureSpec.makeMeasureSpec(0,
                View.MeasureSpec.UNSPECIFIED);
        view.measure(w, h);
        params.height = (int) getHeightSize(hight);
//	    view.setLayoutParams(params);
        view.requestLayout();
        view.invalidate();
    }

    /**
     * 按屏幕大小比例修改控件的宽  高不变
     *
     * @param view
     * @param width
     */
    public static void changeW(View view, int width) {
        LayoutParams params = (LayoutParams) view.getLayoutParams();
        params.width = (int) getHeightSize(width);
//	    view.setLayoutParams(params);
        view.requestLayout();
        view.invalidate();
    }

    /**
     * 宽度扩大全屏，高度按照比列扩大
     *
     * @param view
     * @param Proportion
     */
    public static void changeScreenW_H(View view, float Proportion) {
        LayoutParams params =  view.getLayoutParams();
        if(params == null){
            return;
        }
        if (widthPixels == 0)
            getScreenSize();
        params.height = (int) (widthPixels * Proportion);
        params.width = widthPixels;
        view.requestLayout();
        view.invalidate();
    }

    /**
     * 按图片大小比例修改控件 宽为屏幕宽度(去除外边距) 高相应的扩大
     */
    public static void changeImageHW(View view, int margin, float hight, float width) {
        LayoutParams params = (LayoutParams) view.getLayoutParams();
        if (widthPixels == 0)
            getScreenSize();
        params.height = (int) (((widthPixels - margin * 2) / width) * hight);
        params.width = widthPixels - margin * 2;
        view.requestLayout();
        view.invalidate();
    }

    /**
     * 不按屏幕高度比例修改控件的高 宽不变
     *
     * @param view
     * @param hight
     */
    public static void changeViewH(View view, int hight) {
        LayoutParams params = (LayoutParams) view.getLayoutParams();
        params.height = hight;
//	    view.setLayoutParams(params);
        view.requestLayout();
        view.invalidate();
    }

    /**
     * 修改控件的宽 高不变
     *
     * @param view
     * @param width
     */
    public static void changeViewW(View view, int width) {
        LayoutParams params = (LayoutParams) view.getLayoutParams();
        params.width = width;
//	    view.setLayoutParams(params);
        view.requestLayout();
        view.invalidate();
    }

    /**
     * 得到控件的高
     *
     * @param view
     */
    public static int getViewH(View view) {
        int w = View.MeasureSpec.makeMeasureSpec(0,
                View.MeasureSpec.UNSPECIFIED);
        int h = View.MeasureSpec.makeMeasureSpec(0,
                View.MeasureSpec.UNSPECIFIED);
        view.measure(w, h);
        return view.getMeasuredHeight();
    }

    /**
     * 得到控件的宽
     *
     * @param view
     */
    public static int getViewW(View view) {
        int w = View.MeasureSpec.makeMeasureSpec(0,
                View.MeasureSpec.UNSPECIFIED);
        int h = View.MeasureSpec.makeMeasureSpec(0,
                View.MeasureSpec.UNSPECIFIED);
        view.measure(w, h);
        return view.getMeasuredWidth();
    }

    /**
     * 重新计算listview的高度
     *
     * @param listView
     * @param height   额外增加的高度
     */
    public static void setListViewHeightBasedOnChildren(ListView listView, int height) {
        if (listView == null) {
            return;
        }
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            // pre-condition
            return;
        }

        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            if (listItem == null) {
                continue;
            }
            if (listItem.getLayoutParams() == null) {
                listItem.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
            }
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }
        LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight
                + (listView.getDividerHeight() * (listAdapter.getCount() - 1)) + height;
        listView.setLayoutParams(params);
//		listView.requestLayout();
//		listView.invalidate();
    }

    /**
     * 返回View底部到屏幕底部的高度
     *
     * @param view
     * @return
     */
    public static int getScreenOnYOverHeight(View view) {
        float viewOnHg = view.getY() + view.getHeight();
        int height = (int) (getHeightPixels() - viewOnHg);
        return height;
    }



    public static void changeViewAuto720(View view, int width, int height) {
        LayoutParams params = (LayoutParams) view.getLayoutParams();
        if (widthPixels == 0)
            getScreenSize();
        params.width = (int) ((widthPixels / 720.00f) * width);
        params.height = (int) ((widthPixels / 720.00f) * height);
//	    view.setLayoutParams(params);
        view.requestLayout();
        view.invalidate();
    }

    /**
     * 获得屏幕三分之一的宽度的 px-里边有三个子view
     * @param context
     * @param paddingdp  子view之间的间距
     * @param padding2dp 父控件对于屏幕的padding
     * @return
     */
    public static float changeView30Per(Context context,float paddingdp,float padding2dp){
        int px1 = dp2px(context, paddingdp);
        int px2 = dp2px(context, padding2dp);
        int widthPixels = getWidthPixels();
        int tempWidth = widthPixels - (px1 * 2);
        int width30Perpx = (tempWidth - px2) / 3;
        return width30Perpx;
    }









    /**
     * 更新view的LayoutParams 宽高的方法
     * @param view
     * @param width px
     * @param height    px
     */
    public static void changeViewWH(View view, int width, int height){
        LayoutParams params =  view.getLayoutParams();
        params.width = width;
        params.height = height;
        view.requestLayout();
        view.invalidate();
    }

}
